/**
 * 
 */
package com.zc.BankCreditManagement.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.zc.BankCreditManagement.BasicDAO.BasicDAO;
import com.zc.BankCreditManagement.JavaBean.CustomerCreditLoan;

/**
 * @author ZhangCheng
 * @time 2019年9月23日
 */
public class CustomerCreditLoanDAO extends BasicDAO<CustomerCreditLoan> {

	/**
	 * CustomerCreditLoanDAO无参构造 重载父类构造器
	 */
	public CustomerCreditLoanDAO() {
		super();
	}

	/**
	 * CustomerCreditLevelHistoryDAO有参构造 重载父类构造器
	 */
	public CustomerCreditLoanDAO(String url, String user, String pwd) {
		super(url, user, pwd);
	}
	
	/**
	 * 新增新信用贷款条数，并插入到数据库中，参数是Customer对象
	 */
	public int insertNewCreLoan(CustomerCreditLoan cusCLoan) {
		int count = 0;
		String sql = "Insert into CustomerCreditLoan values(?,?,?,?,?)";
		try {
			count = super.updateDML(sql, cusCLoan.getCreditLoanId(), cusCLoan.getCusId(), cusCLoan.getLoanMoney(),cusCLoan.getBeginDate(), 0);

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	
	/**
	 * @apiNote 根据用户id删除客户当前客户信用贷款信息表示已还款 当数据库内不存在该客户时 返回 0 此时代表已被删除或者根本不存在 若要判断是其中哪一种状态
	 *          请使用get方法并设置excludeDelete为false
	 */
	public int deleteCusLoanById(int cusId) {
		int count = 0;
		String sql = "update CustomerCreditLoan set isDelete = 1 where cusid = ?";
		try {
			count = super.updateDML(sql, cusId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	
	/**
	 * getCreditLoaningById(String cusId) 通过用户Id获取该用户当前正在进行的贷款
	 */
	public CustomerCreditLoan getCreditLoaningById(int cusId) {
		ArrayList<CustomerCreditLoan> cCLoan = getCreditLoansById(cusId,true);
		if(cCLoan == null) {
			
			return null;
		}else {
		System.out.println("get "+cCLoan.get(0).getInfo());
			return cCLoan.get(0);
		}
		

	}
	
	/**
	 * getCreditLoansById(String cusId) 通过用户Id获取该用户所有贷款信息
	 */
	public ArrayList<CustomerCreditLoan> getCreditLoansById(int cusId, boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from CustomerCreditLoan where cusid = ? ");
		if (excludeDelete)
			sql.append("and isdelete = 0");// 如果排除被删除的，那么只返回未被删除的,即正在进行的贷款
		ArrayList<CustomerCreditLoan> c = getInfoFromDB(sql.toString(), cusId);
		if (c.size() == 0) {
			return null;
		} else {
			return c;
		}

	}
	@Override
	public ArrayList<CustomerCreditLoan> resultSetToArrayList(ResultSet re) {
		ArrayList<CustomerCreditLoan> cusCLHis = new ArrayList<CustomerCreditLoan>();

		try {
			while (re.next()) {
				cusCLHis.add(new CustomerCreditLoan(re.getInt("cusId"),re.getString("creditLoanId"),re.getInt("loanMoney"),re.getDate("beginDate")));
//				System.out.println(c.get(0).getInfo());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		super.closeAll();
		return cusCLHis;
	}

}
